package cn.tlb.ui.widget;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListPopupWindow;
import android.widget.PopupWindow;

import java.util.ArrayList;

import cn.tlb.xutilsdownload.R;

public class PopupDemoActivity extends AppCompatActivity {
    private Button button;
    ListPopupWindow lpw;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_popuo_demo);
        button = (Button) findViewById(R.id.button8);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        View view = getLayoutInflater().inflate(R.layout.popuo_layout, null);

        final PopupWindow popupWindow
                = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        popupWindow.setTouchable(true);
        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.gray_44));
        popupWindow.setAnimationStyle(R.style.MyPopuPAnimation);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.showAtLocation(button, Gravity.BOTTOM, 20, 300);
            }
        });

        view.findViewById(R.id.transLayout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (popupWindow.isShowing()) {
                    popupWindow.dismiss();
                }
            }
        });


        //--------------ListPopupWindow--------------
        lpw = new ListPopupWindow(this);
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add("item "+i);
        }
        lpw.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list));


        final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });
    }

    // 显示一个List PopupWindow
    public void listPopup(View view) {
        //需指定宽高
        lpw.setWidth(view.getWidth());
        lpw.setHeight(400);
        //设置ListPopupWindow的固定位置(当前View)
        lpw.setAnchorView(view);
        lpw.show();
    }

    public void showDialog(View view) {
        ProgressDialog dialog = new ProgressDialog(this);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.indeterminate));
        dialog.setProgressDrawable(getResources().getDrawable(R.drawable.indeterminate));
        dialog.show();
    }
}
